<?php

    require_once 'Connection.php';

    class FullName {

        private $NameId;
        private $FirstName;
        private $LastName;
        private $MiddleInitial;

        public function __construct($nameid, $firstname, $lastname, $middleinitial) {
            $this->NameId = $nameid;
            $this->FirstName = $firstname;
            $this->LastName = $lastname;
            $this->MiddleInitial = $middleinitial;
        }

        public function __get($key) {
            if(property_exists($this, $key)) {
                return $this->{$key};
            }
            else {
                return NULL;
            }
        }

        public function __set($key, $value) {
            throw new Exception('Class is immutable.');
        }

        public function ToString() {
            return $this->FirstName . " " . $this->MiddleInitial . ". " . $this->LastName;
        }

        public static function InsertFullName($fullname) {
            Connection::Connect();
            if(($try = FullName::ReadFullName($fullname->NameId)) != NULL) {
                return $try->NameId;
            }

            $sql = mysql_query(
                "
                    INSERT INTO 
                    FullName (
                        FirstName, 
                        LastName, 
                        MiddleInitial 
                    ) 
                    VALUES (
                        '$fullname->FirstName', 
                        '$fullname->LastName', 
                        '$fullname->MiddleInitial'
                    )
                "
            );
            
            if($sql == FALSE)
                return NULL;

            $sql = mysql_query(
                "SELECT LAST_INSERT_ID() as 'LAST_INSERT_ID'"
            );
            
            if($sql == NULL)
                return NULL;

            $rows = mysql_num_rows($sql);
            if($rows == 1) {
                while($row = mysql_fetch_assoc($sql))
                {
                    return $row['LAST_INSERT_ID'];
                }
            }

            return NULL;
        }

        public static function ReadFullName($fullnameid) {
            Connection::Connect();
            $sql = mysql_query(
                "
                    SELECT 
                        *
                    FROM 
                        FullName 
                    WHERE
                        NameId = '$fullnameid'
                "
            );
            
            if($sql == NULL)
                return NULL;

            $rows = mysql_num_rows($sql);
            if($rows == 1) {
                while($row = mysql_fetch_assoc($sql))
                {
                    $nameid = $row['NameId'];
                    $firstname = $row['FirstName'];
                    $lastname = $row['LastName'];
                    $middleinitial = $row['MiddleInitial'];
                    return new FullName($nameid, $firstname, $lastname, $middleinitial);
                }
            }
            return NULL;
        }

        public static function UpdateFullName($fullname) {
            Connection::Connect();
            $sql = mysql_query(
                "
                    UPDATE 
                        FullName 
                    SET 
                        FirstName = '$fullname->FirstName', 
                        LastName = '$fullname->LastName', 
                        MiddleInitial = '$fullname->MiddleInitial', 
                    WHERE 
                        NameId = '$fullname->NameId'
                " 
            );
            
            if($sql == FALSE)
                return NULL;
            
            return $fullname->NameId;
        }

    }

?>